
const { ccclass, property, executeInEditMode } = cc._decorator;
@ccclass
@executeInEditMode
/**
 * 背景图片不规则抠图
 */
export default class MaskModule extends cc.Component {

    mask = null;

    editor: {
        executeInEditMode: true,
    }

    onLoad() {
        this.mask = this.node.getComponent(cc.Mask);
        let points = this.node.getChildByName('img').getComponent(cc.PhysicsPolygonCollider).points;
        this.updateParentGraphics(points);
        // this.node.getChildByName('img').group = 'wall';
        this.node.getChildByName('img').getComponent(cc.PhysicsPolygonCollider).apply();
    }

    updateParentGraphics(points) {
        this.mask.type = cc.Mask.Type.ELLIPSE;
        this.mask._updateGraphics = () => {
            var graphics = this.mask._graphics;
            graphics.clear();
            graphics.lineCap = cc.Graphics.LineCap.ROUND;
            graphics.lineWidth = 5;
            graphics.moveTo(points[0].x, points[0].y);
            for (var i = 1; i <= points.length; i++) {
                if (i == points.length) {
                    graphics.lineTo(points[0].x, points[0].y);
                } else {
                    graphics.lineTo(points[i].x, points[i].y);
                }
            }
            graphics.fill();
        };
        this.mask._updateGraphics();
    }

}



